﻿/**
 * ...
 * @author Lobanov A.V.
 */

package view.module
{
	import org.casalib.display.CasaMovieClip;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import view.components.background.BackgroundSlideshow;
	import view.components.background.Line45Degree;
	import view.components.background.PatternBg;
	
	import flash.display.MovieClip;
	
	/**
	 * A Mediator for interacting with the ...
	 */
	public class BackgroundMediator extends Mediator implements IMediator 
	{
		
		// Cannonical name of the Mediator
		public static const NAME:String = "BackgroundMediator";
		
		private var _viewComponent:MovieClip;
		private var _patternBg:PatternBg;
		private var _bgSlideshow:BackgroundSlideshow;
		private var _line45degree:CasaMovieClip;
		
		public function BackgroundMediator( viewComponent:Object ) 
		{
			// pass the viewComponent to the superclass where 
			// it will be stored in the inherited viewComponent property
			super( NAME, viewComponent );
			
			_viewComponent = viewComponent as MovieClip;
		}
		
		private function initViewComponents($xmlData:XML):void {
			//init components
			_bgSlideshow = new BackgroundSlideshow($xmlData);
			_patternBg = new PatternBg();
			_line45degree = new Line45Degree();
			
			_viewComponent.addChild(_bgSlideshow);
			_viewComponent.addChild(_patternBg);
			_viewComponent.addChild(_line45degree);
		}
        
		/**
		 * List all notifications this Mediator is interested in.
		 * <P>
		 * Automatically called by the framework when the mediator
		 * is registered with the view.</P>
		 * 
		 * @return Array the list of Nofitication names
		 */
		override public function listNotificationInterests():Array 
		{
			return [ApplicationFacade.STAGE_IS_RESIZE,
					ApplicationFacade.CORE_DATA_IS_LOAD];
		}
		
		/**
		 * Handle all notifications this Mediator is interested in.
		 * <P>
		 * Called by the framework when a notification is sent that
		 * this mediator expressed an interest in when registered
		 * (see <code>listNotificationInterests</code>.</P>
		 * 
		 * @param INotification a notification 
		 */
		override public function handleNotification( note:INotification ):void 
		{
			switch ( note.getName() ) 
			{           
				case ApplicationFacade.STAGE_IS_RESIZE:
					_patternBg.updateBg(
						(note.getBody() as Array)[0] as Number, 
						(note.getBody() as Array)[1] as Number
					);
					
					_bgSlideshow.setPosition(
						(note.getBody() as Array)[0] as Number, 
						(note.getBody() as Array)[1] as Number
					);
					
					_line45degree.startLine(
						(note.getBody() as Array)[0] as Number, 
						(note.getBody() as Array)[1] as Number
					);
				break;
				
				case ApplicationFacade.CORE_DATA_IS_LOAD:
					initViewComponents(note.getBody() as XML);
				break;
				
				default:
					break;		
			}
		}
	}
}